package com.leyou.item.controller;

import com.leyou.common.pojo.PageResult;
import com.leyou.item.pojo.Brand;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: taft
 * @Date: 2018-12-18 9:11
 */
@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping("page")
    public ResponseEntity<PageResult<Brand>> queryBrandByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy", required = false) String sortBy,
            @RequestParam(value = "desc", defaultValue = "false") Boolean desc,
            @RequestParam(value = "key", required = false) String  key){
        PageResult<Brand> pageResult = this.brandService.queryBrandByPage(page,rows,sortBy,desc,key);
        if (pageResult != null && null!=pageResult.getItems() && 0!=pageResult.getItems().size()) {
            return ResponseEntity.ok(pageResult);
        }

        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @PostMapping
    public ResponseEntity<Void> saveBrand(Brand brand,@RequestParam("cids")List<Long> cids){

        //保存品牌数据
        this.brandService.saveBrand(brand,cids);

        //没问题返回201
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping
    public ResponseEntity<Void> updateBrand(Brand brand,@RequestParam("cids")List<Long> cids){

        //保存品牌数据
        this.brandService.updateBrand(brand,cids);

        //没问题返回201
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @GetMapping("cid/{cid}")
    public ResponseEntity<List<Brand>> queryBrandByCategory(@PathVariable("cid")Long cid){
        List<Brand> brands = this.brandService.queryBrandByCategory(cid);
        if (brands != null && 0!= brands.size()) {
            return ResponseEntity.ok(brands);
        }

        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @GetMapping("bid")
    public ResponseEntity<List<Brand>> getBrandById(@RequestParam("bids")List<Long> bids){
        List<Brand> brands = this.brandService.getBrandsByIds(bids);
        if (brands != null && 0!= brands.size()) {
            return ResponseEntity.ok(brands);
        }
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
